package com.project.common.pagination;

public final class PaginationContext {
    private static final ThreadLocal<Pagination> CURRENT_PAGINATION = new ThreadLocal<>();

    private PaginationContext() {
    }

    public static PaginationContext currentContext() {
        return Holder.Instance.getPaginationContext();
    }

    public void beginPagination(int pageIndex, int pageSize) {
        Pagination pagination = new Pagination();
        pagination.setPageIndex(pageIndex);
        pagination.setPageSize(pageSize);
        CURRENT_PAGINATION.set(pagination);
    }

    public Pagination getPagination() {
        return CURRENT_PAGINATION.get();
    }

    public void releasePagination() {
        CURRENT_PAGINATION.remove();
    }

    private enum Holder {
        Instance;

        private PaginationContext paginationContext;

        Holder() {
            this.paginationContext = new PaginationContext();
        }

        public PaginationContext getPaginationContext() {
            return this.paginationContext;
        }
    }
}

